#ifndef NUDUSENV_H
#define NUDUSENV_H

// Nudus-Script
//
// Copyright (C) 2012  Damian Balandowski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "Nudus.h"

class NudusEnv
{
public:
	NudusEnv() {
		pos = 0; 
		beginf = 0; 
		a_func = 0;
     there_is_var = false;
	}

	virtual void init() { /* */ }
	virtual void update() { /* */ }

	void parse()
	{
		data = loadFile(filename);

		while(++pos < data.size())
		{
			if(data[pos] == '(')
			{
				attr = pos;
			}
      else if(data[pos] == ':')
      {
        var_attr = pos;
        there_is_var = true;
      }
			else if(data[pos] == ',' && !isStringDef)
			{
				commas.push_back(pos);
			}
			else if (data[pos] == ')')
			{
				attr_end = pos;
			}
      else if(data[pos] == '=')
      {
      }
			else if(data[pos] == ';')
			{
         // if(there_is_var)
         // {
         //   variable v;
         //   v.name = data.substr ( var_attr + 1, pos - var_attr - 1 );
         //   v.values.push_back("null");
         //   variables.push_back(v);
         // }
         // else ...
         
				function* func = new function;
				func->name = data.substr(beginf,attr-beginf);

				functions.push_back(*func);

				beginf = pos+1;
			}
		}
	}

	void create()
	{
		init();
		parse();
		update();
	}

	int countFunctions() { return functions.size(); }
  int countParamsInFunction(int id) { return functions[id].args.size(); }
  
	void setFile(char *f) { filename = f; }

	string getFunction(int id) 
	{
		return functions[id].name;
	}

	string getParam(int id, int param)
	{
		return functions[id].args[param];
	}
   
  int getParamToInt(int id, int param)
  {
    return atoi(functions[id].args[param].c_str());
  }

private:
	char *filename;
	string data;

	int beginf;

	int attr;
	int attr_end;
  bool there_is_var;
  int var_attr;
	int pos;
  bool isStringDef;
	int a_func;
	vector <function> functions;
  vector <variable> variables;

	vector <int> commas;
};

#endif